package elec315.chatroom.core.dispatcher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import elec315.chatroom.core.processor.MessageProcessor;
import elec315.chatroom.msg.BaseMsg;
import elec315.chatroom.msg.MsgId;
import elec315.chatroom.net.SocketHandler;

abstract public class MessageDispatcher extends Thread {

	private static final Log logger = LogFactory.getLog(MessageDispatcher.class);

	private final MessageProcessor processor;

	public MessageDispatcher(MessageProcessor messageProcessor) {
		super();
		this.processor = messageProcessor;
	}

	/**
	 * Handle received request or response message.
	 * 
	 * @param baseMsg
	 * @param socketHandler
	 */
	public void onMessage(BaseMsg baseMsg, SocketHandler socketHandler) {
		MsgId msgId = baseMsg.getMsgId();
		logger.debug("Received msgId: " + msgId);
		processor.process(baseMsg, socketHandler);
	}

}
